package com.weigee.ktv.m.retrofit;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.l.d.app.library.adapter.MyListAdapter;
import com.l.d.app.library.adapter.RvListAdapter;
import com.l.d.app.library.util.ParseUtil;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import skin.support.widget.SkinCompatTextView;

/**
 * Created by linde on 2017/04/24.
 * <p/>
 */
final class ApiRequestImpl
{
    private void notifyCompletely(final ApiRequest.Back back, final boolean append)
    {
        if (back != null)
        {
            back.onCompletely(append, append);
        }
    }

    void balance(final SkinCompatTextView tv)
    {
        Call<ResponseBody> call = RetrofitUtil.INSTANCE.getApiService().balance();
        call.enqueue(new Callback<ResponseBody>()
        {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
            {
                try
                {
                    final String result = response.body().string();
                    if (TextUtils.isEmpty(result))
                    {
                        tv.setText(String.valueOf("¥0.00"));
                    }
                    float balance = JSONObject.parseObject(result).getFloatValue("balance");
                    if (balance < 0)
                    {
                        balance = 0;
                    }
                    if (tv != null)
                    {
                        tv.setText(String.valueOf("¥" + ParseUtil.numPoint(balance, 2)));
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t)
            {
                t.printStackTrace();
                try
                {
                    if (tv != null)
                    {
                        tv.setText(String.valueOf("¥0.00"));
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    <Entity> void rechargeReturn(final MyListAdapter<Entity, ?> adapter, final boolean append, final Class<Entity> clazz)
    {
        RetrofitUtil.INSTANCE.getApiService().rechargeReturn().enqueue(new Callback<ResponseBody>()
        {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
            {
                try
                {
                    final String result = response.body().string();
                    if (result == null)
                    {
                        return;
                    }
                    JSONArray array = JSONArray.parseArray(result);
                    if (adapter != null)
                    {
                        if (!append)
                        {
                            adapter.clear();
                        }
                        adapter.add(array, clazz);
                        adapter.notifyDataSetChanged();
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t)
            {
                t.printStackTrace();
            }
        });
    }

    <Entity> void transactionHistory(final RvListAdapter<Entity, ?> adp, final boolean append, final Class<Entity> clazz, final ApiRequest.Back back)
    {
        RetrofitUtil.INSTANCE.getApiService().transactionHistory().enqueue(new Callback<ResponseBody>()
        {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
            {
                try
                {
                    final String respStr = response.body().string();
                    if (adp != null)
                    {
                        if (!append)
                        {
                            adp.clear();
                        }
                        if (!TextUtils.isEmpty(respStr))
                        {
                            JSONArray array = JSONArray.parseArray(respStr);
                            adp.add(array, clazz);
                        }
                        adp.notifyDataSetChanged();
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
                notifyCompletely(back, append);
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t)
            {
                t.printStackTrace();
                notifyCompletely(back, append);
            }
        });
    }

    <Entity> void supermarketChooseGoodsLeftData(final RvListAdapter<Entity, ?> adp, final boolean append, final Class<Entity> clazz, final ApiRequest.Back back)
    {
        RetrofitUtil.INSTANCE.getApiService().supermarketChooseGoodsLeftData().enqueue(new Callback<ResponseBody>()
        {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
            {
                try
                {
                    final String respStr = response.body().string();
                    if (adp != null)
                    {
                        if (!append)
                        {
                            adp.clear();
                        }
                        if (!TextUtils.isEmpty(respStr))
                        {
                            JSONArray array = JSONArray.parseArray(respStr);
                            adp.add(array, clazz);
                        }
                        adp.notifyDataSetChanged();
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
                notifyCompletely(back, append);
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t)
            {
                t.printStackTrace();
                notifyCompletely(back, append);
            }
        });
    }

    <Entity> void supermarketChooseGoodsRightData(final RvListAdapter<Entity, ?> adp, final boolean append, final Class<Entity> clazz, final ApiRequest.Back back)
    {

    }
}
